package com.javaio;

import java.nio.ByteBuffer;

/**
 * @program: study
 * @className: ByteBufferTest
 * @description: TODO
 * @author: kukuxiahuni
 * @create: 2020-01-02 16:20
 * @version: v1.0
 **/
public class ByteBufferTest {
    public static void main(String[] args) {
        ByteBufferTest byteBufferTest = new ByteBufferTest();
        byteBufferTest.test();
    }

    /**
     * 功能描述: 测试ByteBuffer mark < position < limit < capacity的场景
     *
     * @param:
     * @return:
     * @auther: kukuxiahuni
     * @date: 2020/1/2 4:21 下午
     * @modify_auther: kukuxiahuni
     * @modify_time: 2020/1/2 4:21 下午
     **/
    public final void test() {

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put("我们都是中国人".getBytes());

        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.position());
        System.out.println("*****************************");
        byteBuffer.flip();
        System.out.println("*****************************");
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.position());

        byteBuffer.getChar();
        System.out.println("*****************************");
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.position());


        byteBuffer.compact();
        System.out.println("*****************************");
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.position());

        byteBuffer.put("fafaf".getBytes());
        System.out.println("*****************************");
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.position());
        byteBuffer.getChar();
//        byteBuffer.compact();
        System.out.println("*****************************");
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println(byteBuffer.position());
    }
}
